package guiserversimulation;

import guiserver.ControlsManager;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;

public class DrawablePanel extends JPanel {

    public DrawablePanel() {
       ControlsManager.GetInstance().InitJPanel(this);
       requestFocus();
       addMouseListener(new java.awt.event.MouseAdapter() {
           
            @Override
            public void mousePressed(MouseEvent evt) {
                ControlsManager.GetInstance().OnMouseDownHandler(evt.getX(), evt.getY());
            }

            @Override
            public void mouseReleased(MouseEvent evt) {
                ControlsManager.GetInstance().OnMouseUpHandler(evt.getX(), evt.getY());
            }

            @Override
            public void mouseMoved(MouseEvent evt) {
                ControlsManager.GetInstance().OnMouseMovedHandler(evt.getX(), evt.getY());
            }

            @Override
            public void mouseClicked(MouseEvent evt) {
                ControlsManager.GetInstance().OnMouseClickedHandler(evt.getX(), evt.getY());
            }
       });

       addKeyListener(keyListener);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        ControlsManager.GetInstance().Redraw(g);
    }

    private static KeyListener keyListener = new java.awt.event.KeyListener() {

        @Override
        public void keyTyped(KeyEvent evt) {

        }

        @Override
        public void keyPressed(KeyEvent evt) {
            ControlsManager.GetInstance().OnKeyPressed(
               KeyEvent.getKeyText(evt.getKeyCode()), KeyEvent.getKeyModifiersText(evt.getModifiers()));
        }

        @Override
        public void keyReleased(KeyEvent evt) {
            ControlsManager.GetInstance().OnKeyReleased(
               KeyEvent.getKeyText(evt.getKeyCode()), KeyEvent.getKeyModifiersText(evt.getModifiers()));
        }
    };

    public static KeyListener GetKeyListener() {
        return keyListener;
    }
}
